package com.ruoyi.lvlian.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.MD5Utils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.lvlian.dto.WarehouseRefDto;
import com.ruoyi.lvlian.util.ExcelUtil;
import com.ruoyi.lvlian.vo.ImagesVo;
import com.ruoyi.lvlian.vo.WarehouseRefVo;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.lvlian.mapper.WarehouseRefMapper;
import com.ruoyi.lvlian.domain.WarehouseRef;
import com.ruoyi.lvlian.service.IWarehouseRefService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 销售物流关联采购物流Service业务层处理
 *
 * @author liyuxi
 * @date 2024-12-26
 */
@Service
public class WarehouseRefServiceImpl implements IWarehouseRefService {
    @Autowired
    private WarehouseRefMapper warehouseRefMapper;

    /**
     * 查询销售物流关联采购物流
     *
     * @param id 销售物流关联采购物流主键
     * @return 销售物流关联采购物流
     */
    @Override
    public WarehouseRef selectWarehouseRefById(String id) {
        return warehouseRefMapper.selectWarehouseRefById(id);
    }

    /**
     * 查询销售物流关联采购物流列表
     *
     * @param warehouseRef 销售物流关联采购物流
     * @return 销售物流关联采购物流
     */
    @Override
    public List<WarehouseRefVo> selectWarehouseRefList(WarehouseRefDto warehouseRef) {
        return warehouseRefMapper.selectWarehouseRefList(warehouseRef);
    }

    /**
     * 新增销售物流关联采购物流
     *
     * @param warehouseRef 销售物流关联采购物流
     * @return 结果
     */
    @Override
    public int insertWarehouseRef(WarehouseRef warehouseRef) {
        return warehouseRefMapper.insertWarehouseRef(warehouseRef);
    }

    /**
     * 修改销售物流关联采购物流
     *
     * @param warehouseRef 销售物流关联采购物流
     * @return 结果
     */
    @Override
    public int updateWarehouseRef(WarehouseRef warehouseRef) {
        return warehouseRefMapper.updateWarehouseRef(warehouseRef);
    }

    /**
     * 批量删除销售物流关联采购物流
     *
     * @param ids 需要删除的销售物流关联采购物流主键
     * @return 结果
     */
    @Override
    public int deleteWarehouseRefByIds(String[] ids) {
        return warehouseRefMapper.deleteWarehouseRefByIds(ids);
    }

    /**
     * 删除销售物流关联采购物流信息
     *
     * @param id 销售物流关联采购物流主键
     * @return 结果
     */
    @Override
    public int deleteWarehouseRefById(String id) {
        return warehouseRefMapper.deleteWarehouseRefById(id);
    }

    @Override
    public List<WarehouseRef> importInfo(MultipartFile file, String sheetName, int rowNum) throws Exception {
        byte[] fileBytes = file.getBytes();
        List<WarehouseRef> invoiceRecyclingInfos = ExcelUtil.readExcel(fileBytes, WarehouseRef.class, sheetName, rowNum);
        return invoiceRecyclingInfos;
    }

    @Override
    public int bachSave(List<WarehouseRef> dtoList) {
        int saveBatch = 0;
        List<WarehouseRef> newList = new ArrayList<>();
        if (!dtoList.isEmpty()) {
            for (WarehouseRef po : dtoList) {
                if (StringUtils.isEmpty(po.getInNum()) || StringUtils.isEmpty(po.getOutNum())
                        || StringUtils.isEmpty(po.getBuyerName()) || StringUtils.isEmpty(po.getSellerName())
                        || StringUtils.isEmpty(po.getInDate()) || StringUtils.isEmpty(po.getOutDate())) {
                    continue;
                }
                po.setId(MD5Utils.md5(po.getInNum() + po.getOutNum() + po.getBuyerName() + po.getSellerName() + po.getInDate() + po.getOutDate()));

                po.setCreateDate(DateUtils.getCurrentTime());
                po.setCreateUser(SecurityUtils.getUsername());
                newList.add(po);
            }
            saveBatch = warehouseRefMapper.batchSave(newList);
        }
        return saveBatch;
    }

    @Override
    public int syncImgs(String outNum, String sellerContractNum, String outDate, String inNum, String buyerContractNum, String inDate, String col) {
        ImagesVo imagesVo = warehouseRefMapper.querySellerImages(outNum, sellerContractNum, outDate, col);
        int result = 0;
        if (StringUtils.isNotEmpty(imagesVo.getLoadImg())) {
            int i = warehouseRefMapper.syncImgs(inNum, buyerContractNum, inDate, "load_img", imagesVo.getLoadImg());
            result = i + result;
        }
        if (StringUtils.isNotEmpty(imagesVo.getLoadWeightImg())) {
            int i = warehouseRefMapper.syncImgs(inNum, buyerContractNum, inDate, "load_weight_img", imagesVo.getLoadWeightImg());
            result = i + result;
        }
        if (StringUtils.isNotEmpty(imagesVo.getInGrossWeightImg())) {
            int i = warehouseRefMapper.syncImgs(inNum, buyerContractNum, inDate, "in_gross_weight_img", imagesVo.getInGrossWeightImg());
            result = i + result;
        }
        if (StringUtils.isNotEmpty(imagesVo.getOutGrossWeightImg())) {
            int i = warehouseRefMapper.syncImgs(inNum, buyerContractNum, inDate, "out_gross_weight_img", imagesVo.getOutGrossWeightImg());
            result = i + result;
        }
        if (StringUtils.isNotEmpty(imagesVo.getOutWeighNumImg())) {
            int i = warehouseRefMapper.syncImgs(inNum, buyerContractNum, inDate, "out_weigh_num_img", imagesVo.getOutWeighNumImg());
            result = i + result;
        }
        if (StringUtils.isNotEmpty(imagesVo.getInWeightImg())) {
            int i = warehouseRefMapper.syncImgs(inNum, buyerContractNum, inDate, "in_weight_img", imagesVo.getInWeightImg());
            result = i + result;
        }
        if (StringUtils.isNotEmpty(imagesVo.getUnloadImg())) {
            int i = warehouseRefMapper.syncImgs(inNum, buyerContractNum, inDate, "unload_img", imagesVo.getUnloadImg());
            result = i + result;
        }

        return result;
    }
}
